/*
 * KIMWONSORK@GMAIL.COM
 * COPYRIGHT CRAYONSOFT All rights reserved.
 * 2013오후 8:05:15
 * kws
 */

package com.crayon.util;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import org.apache.commons.lang3.StringUtils;


public class UIBIND {

	public static void bindView(View layout,int nId,View.OnClickListener clickListener){
		View v = layout.findViewById(nId);
		if(v!=null){
			v.setOnClickListener(clickListener);

		}
	}
	public static void bindView(Activity activity,int nId,View.OnClickListener clickListener){
		View v = activity.findViewById(nId);
		if(v!=null){
			v.setOnClickListener(clickListener);
		}
	}
	public static TextView bindTextA(Activity activity,int nId,CharSequence msg){
		TextView v = (TextView)activity.findViewById(nId);
		if(v!=null && msg!=null){
			v.setText(msg);
		}
		return v;
	}

	public static String getTextValue(View view,int nId){
		View v = view.findViewById(nId);
		if(v!=null && v instanceof EditText){
			EditText editText = (EditText)v;

			return StringUtils.isEmpty(editText.getText().toString())?null:editText.getText().toString();
		}
		return null;
	}
	public static long getTextValueNumber(View view,int nId){
		String number = getTextValue(view, nId);
		try {

			return Long.parseLong(UTILCurrency.onlyNum(number));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	public static View bindView(View layout,int nId){
		View v = layout.findViewById(nId);
		return v;
	}
	public static void bindViewVisible(View layout,int nId,boolean visibility){
		if(layout!=null){
			View v = bindView(layout, nId);
			if(v!=null) v.setVisibility(visibility?View.VISIBLE:View.GONE);
		}
	}

	public static TextView bindText(View layout,int nId,CharSequence msg){
        if(layout==null) return null;
		View  v = layout.findViewById(nId);
		if(v!=null && msg!=null && v instanceof  TextView){
            ((TextView)v).setText(msg);
            return (TextView)v;
		}
		return null;
	}
	public static void bindTextQueryColor(View layout,int nId,String str,String query){
		TextView textView = bindText(layout, nId, str);
		SpannableStringBuilder sp = new SpannableStringBuilder(str);

		try {
			int index = StringUtils.indexOfAny(str, query.toCharArray());
			GDB.log(UIBIND.class, "index " + index +"\t query " + query +"\t" + query.length() +"\t STR=" + str);
			if(index>-1){
				GDB.log(UIBIND.class, " str " + str + "\t query " + query +"\t index " +index);
				sp.setSpan(new ForegroundColorSpan(Color.YELLOW), index,index+StringUtils.length(query), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				textView.setText(sp);
			}

		} catch (Exception e) {
			// TODO: handle exception
			GDB.log(UIBIND.class, "uibind err " + e.getMessage());
		}

	}


	public static Button bindButton(View layout,int nId,CharSequence text){
		View button = layout.findViewById(nId);
		if(button==null) return null;
		if(button instanceof Button){
			((Button)button).setText(text);
			return (Button)button;
		}else if(button instanceof ImageButton){
			return (Button)button;
		}
		return null;

	}

	public static View bindViewBackgroundColor(View layout,int nId,int color){
		View view = layout.findViewById(nId);
		if(view!=null){
			view.setBackgroundColor(color);
		}
		return view;
	}
	public static View bindViewBackgroundResource(View layout,int nId,int resid){
		View view = layout.findViewById(nId);
		if(view!=null){
			view.setBackgroundResource(resid);
		}
		return view;
	}

	public static CheckBox bindCheckBoxCheck(View layout,int nId,boolean checked){
		View view = layout.findViewById(nId);
		if(view instanceof CheckBox){
			((CheckBox)view).setChecked(checked);
			return (CheckBox)view;
		}
		return null;
	}
	public static CheckBox bindCheckBox(View layout,int nId){
		View view = layout.findViewById(nId);
		if(view instanceof CheckBox){

			return (CheckBox)view;
		}
		return null;
	}

	public static void bindImageView(View layout,int nId,int resId){
		View view = bindView(layout, nId);
		if(view instanceof ImageView){
			((ImageView)view).setImageResource(resId);
		}

	}
	
	public static void bindImageViewBitmap(View layout,int nId,Bitmap bitmap){
		View view = bindView(layout, nId);
		if(view instanceof ImageView){
			((ImageView)view).setImageBitmap(bitmap);
		}

	}
	public static void bindImageViewBitmapArray(View layout,int nId,byte[] bitarray) throws Exception{
		View view = bindView(layout, nId);
		if(view instanceof ImageView){
			ImageView imageView = (ImageView)view;
			imageView.setImageBitmap(BitmapFactory.decodeByteArray(bitarray, 0, bitarray.length));
		}

	}


	public static void bindChildButtonAction(View view,OnClickListener listener){
		if(view instanceof ViewGroup){
			ViewGroup group = (ViewGroup)view;
			for(int i = 0;i < group.getChildCount();i++){
//				View child = group.getchil
			}
		}
	}

}
